<?php

/**
 * view actions.
 *
 * @package    worldmap
 * @subpackage view
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class viewActions extends sfActions
{
  protected function i2cRealip()
  {
    // No IP found (will be overwritten by for
    // if any IP is found behind a firewall)
    $ip = false;

    // If HTTP_CLIENT_IP is set, then give it priority
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
      $ip = $_SERVER["HTTP_CLIENT_IP"];
    }

    // User is behind a proxy and check that we discard RFC1918 IP addresses
    // if they are behind a proxy then only figure out which IP belongs to the
    // user.  Might not need any more hackin if there is a squid reverse proxy
    // infront of apache.
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      // Put the IP's into an array which we shall work with shortly.
      $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
      if ($ip) {
        array_unshift($ips, $ip);
        $ip = false;
      }

      for ($i = 0; $i < count($ips); $i++) {
        // Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and
        // 192.168.0.0/16
        if (!preg_match('/^(?:10|172\.(?:1[6-9]|2\d|3[01])|192\.168)\./', $ips[$i])) {
          if (version_compare(phpversion(), "5.0.0", ">=")) {
            if (ip2long($ips[$i]) != false) {
                $ip = $ips[$i];
                break;
            }
          } elseif (ip2long($ips[$i]) != -1) {
            $ip = $ips[$i];
            break;
          }
        }
      }
    }

    // Return with the found IP or the remote address
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  }

 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $lists['users'] = Doctrine::getTable('User')
      ->createQuery('u')
      ->execute();

    $lists['stores'] = Doctrine::getTable('Store')
      ->createQuery('s')
      ->execute();

    $options = array('zoom' => 5);

    $ip = sfConfig::get('mod_view_ip');
    if (!$ip) {
      $ip = self::i2cRealip();
    }
    $geoip = Net_GeoIP::getInstance(sfConfig::get('sf_data_dir').'/geodata/GeoLiteCity.dat');
    $visitor = $geoip->lookupLocation($ip);
    if ($visitor) {
      $options['center_lat'] = $visitor->latitude;
      $options['center_longitude'] =  $visitor->longitude;
    }

    $this->gMap = new GMap($options);

    $gMapIcon = new GMapIcon(
      'nice_icon',
      '/sfEasyGMapPlugin/images/nice_icon.png',
      array(
        'width'=>18,
        'height'=>25,
        'info_window_anchor_x'=>9,
        'info_window_anchor_y'=>25
      )
    );

    // creates two custom event listeners
    $gMapEvent1 = new GMapEvent(
      'mouseover',
      "document.getElementById('console_div').innerHTML = 'Mouse over marker number '+this.num;"
    );
    $gMapEvent2 = new GMapEvent(
      'mouseout',
      "document.getElementById('console_div').innerHTML = '';"
    );

    foreach ($lists as $list) {
      foreach ($list as $entity) {
        $gMapMarker = new GMapMarker($entity->latitude, $entity->longitude,'markers['.$entity->id.']',$gMapIcon);
        $gMapMarker->addHtmlInfoWindow('<b>Name:</b><br />'.$entity->name);
        $gMapMarker->setCustomProperty('num', $entity->id);
        // binds the event listeners to the marker
        $gMapMarker->addEvent($gMapEvent1);
        $gMapMarker->addEvent($gMapEvent2);
        $this->gMap->addMarker($gMapMarker);
      }
    }
  }
}
